﻿Imports DevExpress.XtraEditors
Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraGrid.Views.Grid
Imports System.ComponentModel
Imports DevExpress.XtraGrid

Public Class XuatHang
    Private OldValue, NewValue As Decimal 'giá trị cũ - mới
    Private m_MaPhieu As String 'mã phiếu
    Private m_Flag As Boolean = False 'tránh loop xtragrid
    Private m_FlagValidate As Boolean = False 'bắt lỗi trên lưới 
    Private m_TuNgay, m_DenNgay
    Dim m_Row As DataRow
    Public Sub setMa(ByVal ma As String)
        Me.m_MaPhieu = ma
    End Sub
    Public Overrides Sub setSubSau_TaoMoi()
        Me.setMacDinhCotAn("ma", "XHDP" & System.Guid.NewGuid.ToString)
        Me.TinhtrangISearchLookupEdit.EditValue = "DXL"
        Me.SochungtuITextEdit.EditValue = Me.QueriesTableAdapter1.getSoChungTu_BHBanHang("XHDP", CDate(TuNgayIDateEdit.EditValue), CDate(DenNgayIDateEdit.EditValue)).ToString
        Me.NgaychungtuIDateEdit.EditValue = Today
        Me.NgayhoadonIDateEdit.EditValue = Today
    End Sub
    Private Sub XuatHang_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        If Me.m_MaPhieu IsNot Nothing Then
            Dim position As Integer = Me.BHBanHangBindingSource.Find("ma", Me.m_MaPhieu)
            Me.BHBanHangBindingSource.Position = position
        End If
    End Sub
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.NghiepVuBanHangTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.NghiepVuBanHang)
        Me.DMThoiGianTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMThoiGian)
        Me.DMHangHoaTableAdapter.Fill(Me.PCV_CLINICDataSet.DMHangHoa)
        Me.ThanhToanTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.ThanhToan)
        Me.DMKhachHangTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.DMKhachHang)
        Me.BHBanHangTableAdapter.Fill(Me.PCV_CLINICDataSet.BHBanHang, CDate(Me.TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue), 1)
        Me.BHBanHangChiTietTableAdapter.Fill(Me.PCV_CLINICDataSet.BHBanHangChiTiet, 1, CDate(Me.TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue))
        Me.TinhTrangPhieuTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.TinhTrangPhieu)
        Me.GetTonKho_Lo_HanDung_KhaDungTableAdapter.Fill(Me.PCV_CLINICDataSetTongHop.getTonKho_Lo_HanDung_KhaDung, CDate(Me.TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue))
        Me.setHeaderFooter(Main.TenDoanhNghiep.ToString, Main.DiaChiDoanhNghiep.ToString)
        SplashScreenManager.CloseForm()
    End Sub
    Private Sub XuatHang_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.DMThoiGianISearchLookupEdit.EditValue = "TN"
        loadDuLieu()
    End Sub
    Public Sub New()
        InitializeComponent()
        Me.setSoTabThamChieu(0)
        Me.setSoTab(1)
        Me.setObj(Me.PCV_CLINICDataSet, Me.PCV_CLINICDataSet.BHBanHang, Me.BHBanHangBindingSource, Me.TableAdapterManager, Me.BHBanHangTableAdapter, Me.ManghiepvuISearchLookupEdit, Me.HinhthucthanhtoanISearchLookupEdit)
        Me.setTab1(Me.PCV_CLINICDataSet.BHBanHangChiTiet, Me.FKBHBanHangChima3C34F16FBindingSource, Me.BHBanHangChiTietTableAdapter)
        Me.setColumnsChangeTab1(New ArrayList From {"soluonggiao", "dongiavnd", "chietkhau", "tilethueGTGT", "tilechietkhau"})
        Me.setColumnValueTab1(Me.colEx1ChiTiet01, Me.colthanhtienvnd2)
        Me.setColumnValueTab1(Me.colEx2ChiTiet01, Me.coltienthueGTGT1)
        Me.IGridViewChiTiet01.setBatLoi("soluonggiao", "not 0 and not null")
        Me.IGridViewChiTiet01.setBatLoi("giaban", "not 0 and not null")
    End Sub
    Private Sub MakhachhangISearchLookupEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles MakhachhangISearchLookupEdit.EditValueChanging
        Me.TenkhachhangITextEdit.EditValue = Me.MakhachhangISearchLookupEdit.getRows.Item("ten")
        Me.DiachiITextEdit.EditValue = Me.MakhachhangISearchLookupEdit.getRows.Item("diachi")
        If IsDBNull(Me.MakhachhangISearchLookupEdit.EditValue) = False Then
            Me.GetGia_BangGiaApDungTableAdapter.Fill(Me.PCV_CLINICDataSetTongHop.getGia_BangGiaApDung, CDate(Me.NgaychungtuIDateEdit.EditValue), Me.MakhachhangISearchLookupEdit.EditValue)
        End If
        m_Row = getRow(Me.PCV_CLINICDataSet_Don.NghiepVuBanHang, NghiepVuBanHangBindingSource, "ma", Me.ManghiepvuISearchLookupEdit.EditValue)
        Me.DiengiaiITextEdit.EditValue = m_Row.Item("diengiai").ToString & " " & TenkhachhangITextEdit.EditValue
    End Sub
    Private Sub IGridViewChiTiet01_InitNewRow(sender As Object, e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs) Handles IGridViewChiTiet01.InitNewRow
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("uid", System.Guid.NewGuid)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("sochungtu", Me.SochungtuITextEdit.EditValue)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("soluonggiao", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("thanhtienvnd", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("tilethueGTGT", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("tienthueGTGT", 0)
        Me.IGridViewChiTiet01.SetFocusedRowCellValue("chietkhau", 0)
        Me.setFlagInitNewRowTab1(True)
    End Sub
    Private Sub IGridViewChiTiet01_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridViewChiTiet01.CellValueChanged
        Me.ThanhtienvndITextEdit.EditValue = Me.IGridViewChiTiet01.getSummaryColumn("thanhtienvnd")
        If Me.IGridViewChiTiet01.FocusedColumn.FieldName = "soluonggiao" AndAlso (Not m_Flag) Then
            Try
                Me.m_Flag = True
                Me.NewValue = CDec(Me.IGridViewChiTiet01.GetFocusedRowCellValue("soluonggiao"))
                Dim mahang As String = Me.IGridViewChiTiet01.GetFocusedRowCellValue("mahang")
                Dim solo As String = Me.IGridViewChiTiet01.GetFocusedRowCellValue("solo")
                Dim hsd As Date = CDate(Me.IGridViewChiTiet01.GetFocusedRowCellValue("hsd"))
                Dim soluongton As Decimal = CDec(Me.GetTonKho_Lo_HanDung_KhaDungTableAdapter.get_SoLuongCuoiKy_TonKho_Lo_HanDung_KhaDung(CDate(TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue), mahang, solo, hsd))
                If soluongton < NewValue Then
                    If DevExpress.XtraEditors.XtraMessageBox.Show("Số lượng xuất (" & NewValue & ") lớn hơn số lượng tồn (" & soluongton & ") " & vbNewLine & " Bạn muốn liệt kê hóa đơn xuất để điều chỉnh không ? ", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) = Windows.Forms.DialogResult.Yes Then
                        LietKeHoaDonXuat(mahang, solo, hsd)
                    Else
                        Me.IGridViewChiTiet01.SetFocusedRowCellValue("soluonggiao", OldValue)
                    End If
                End If
            Catch ex As Exception
                Me.NewValue = 0
            End Try
        End If
    End Sub
    Private Sub LietKeHoaDonXuat(ByVal mahang As String, ByVal solo As String, ByVal handung As Date) 'xác định phiếu cuối cùng (trả về dt) chứa mã,số lô,hạn dùng của thuốc đó
        Try
            GetBHBanHangChiTiet_PhieuXuatCuoiTableAdapter.Fill(Me.PCV_CLINICDataSetTongHop.getBHBanHangChiTiet_PhieuXuatCuoi, mahang, solo, handung)
            Dim ma As String = Me.PCV_CLINICDataSetTongHop.getBHBanHangChiTiet_PhieuXuatCuoi.Rows(1).Item("ma")
            Dim ngaychungtu As Date = CDate(Me.PCV_CLINICDataSetTongHop.getBHBanHangChiTiet_PhieuXuatCuoi.Rows(1).Item("ngaychungtu"))
            Me.m_TuNgay = getNgayDauThang(ngaychungtu)
            Me.m_DenNgay = getNgayCuoiThang(ngaychungtu)
            Me.TuNgayIDateEdit.EditValue = m_TuNgay
            Me.DenNgayIDateEdit.EditValue = m_DenNgay
            Me.BHBanHangTableAdapter.Fill(Me.PCV_CLINICDataSet.BHBanHang, CDate(Me.TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue), 1)
            Me.BHBanHangChiTietTableAdapter.Fill(Me.PCV_CLINICDataSet.BHBanHangChiTiet, 1, CDate(Me.TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue))
            Me.TinhTrangPhieuTableAdapter.Fill(Me.PCV_CLINICDataSet_Don.TinhTrangPhieu)
            Me.GetTonKho_Lo_HanDung_KhaDungTableAdapter.Fill(Me.PCV_CLINICDataSetTongHop.getTonKho_Lo_HanDung_KhaDung, CDate(Me.TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue))
            Me.BHBanHangBindingSource.Position = Me.BHBanHangBindingSource.Find("ma", ma) 'di chuyển đến phiếu
        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show("Không có phiếu xuất để liệt kê !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
    Private Sub DMThoiGianISearchLookupEdit_EditValueChanged(sender As Object, e As EventArgs) Handles DMThoiGianISearchLookupEdit.EditValueChanged
        setDMThoiGian(Me.DMThoiGianISearchLookupEdit, TuNgayIDateEdit, DenNgayIDateEdit)
        Me.BarButtonItemLamTuoi.PerformClick()
    End Sub
    Public Overrides Sub setSubTruoc_Luu()
        Me.setSuaTruocKhiLuuTab1("tinhtrang", Me.TinhtrangISearchLookupEdit.EditValue)
        Me.setSuaTruocKhiLuuTab1("ngaychungtu", Me.NgaychungtuIDateEdit.EditValue)
        Me.setSuaTruocKhiLuuTab1("madonvi", Me.MakhachhangISearchLookupEdit.EditValue)
        Me.setSuaTruocKhiLuuTab1("tendonvi", Me.TenkhachhangITextEdit.EditValue)
        Me.setSuaTruocKhiLuuTab1("diachidonvi", Me.DiachiITextEdit.EditValue)
        Me.setSuaTruocKhiLuuTab1("ngayhoadon", Me.NgayhoadonIDateEdit.EditValue)
        Me.setSuaTruocKhiLuuTab1("sohoadon", Me.SohoadonITextEdit.EditValue)
        Dim row As DataRow = getRow(Me.PCV_CLINICDataSet_Don.NghiepVuBanHang, NghiepVuBanHangBindingSource, "ma", Me.ManghiepvuISearchLookupEdit.EditValue)
        Me.setSuaTruocKhiLuuTab1("taikhoanno", row.Item("taikhoanno").ToString)
        Me.setSuaTruocKhiLuuTab1("taikhoanco", row.Item("taikhoanco").ToString)
        Me.setSuaTruocKhiLuuTab1("taikhoanthueGTGT", row.Item("taikhoanvat").ToString)
        Me.setSuaTruocKhiLuuTab1("kyketoan", getRecycleId(CDate(Me.NgaychungtuIDateEdit.EditValue)))
    End Sub
    Public Overrides Sub setSubSau_Luu()
        Me.NewValue = 0
        Me.OldValue = 0
    End Sub
    Protected Overrides Sub BarButtonItemLamTuoi_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs)
        loadDuLieu()
    End Sub
    Private Sub RepositoryItemISearchLookUpEdit_TonTheoLo_HanDung_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles RepositoryItemISearchLookUpEdit_TonTheoLo_HanDung.EditValueChanging
        Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
        Dim rowHandle As Integer = editor.Properties.GetIndexByKeyValue(e.NewValue)
        If getExistColumnValue(Me.IGridViewChiTiet01, "mafifo", editor.Properties.View.GetRowCellValue(rowHandle, "chuoiduynhat")) = False Then
            Me.m_FlagValidate = False
            Me.IGridViewChiTiet01.SetFocusedRowCellValue("mafifo", editor.Properties.View.GetRowCellValue(rowHandle, "chuoiduynhat"))
            Me.IGridViewChiTiet01.SetFocusedRowCellValue("mahang", editor.Properties.View.GetRowCellValue(rowHandle, "mahang"))
            IGridViewChiTiet01.SetFocusedRowCellValue("tenhang", editor.Properties.View.GetRowCellValue(rowHandle, "tenhang"))
            IGridViewChiTiet01.SetFocusedRowCellValue("dvt", editor.Properties.View.GetRowCellValue(rowHandle, "dvt"))
            IGridViewChiTiet01.SetFocusedRowCellValue("solo", editor.Properties.View.GetRowCellValue(rowHandle, "solo"))
            IGridViewChiTiet01.SetFocusedRowCellValue("hsd", editor.Properties.View.GetRowCellValue(rowHandle, "hsd"))
            IGridViewChiTiet01.SetFocusedRowCellValue("quicach", editor.Properties.View.GetRowCellValue(rowHandle, "quicach"))
            'kiểm tra coi có bảng giá tồn tại hay chưa 
            If CInt(Me.GetGia_BangGiaApDungTableAdapter.getTonTaiBangGia(CDate(Me.NgaychungtuIDateEdit.EditValue), Me.MakhachhangISearchLookupEdit.EditValue).ToString) <> 0 Then
                If IsDBNull(editor.Properties.View.GetRowCellValue(rowHandle, "mahang")) = False Then
                    Me.GetGia_BangGiaApDungTableAdapter.Fill(Me.PCV_CLINICDataSetTongHop.getGia_BangGiaApDung, CDate(Me.NgaychungtuIDateEdit.EditValue), Me.MakhachhangISearchLookupEdit.EditValue)
                    Dim row As DataRow = getRow(Me.PCV_CLINICDataSetTongHop.getGia_BangGiaApDung, Me.GetGia_BangGiaApDungBindingSource, "mahang", editor.Properties.View.GetRowCellValue(rowHandle, "mahang"))
                    Try
                        Me.IGridViewChiTiet01.SetFocusedRowCellValue("dongiavnd", CDec(row.Item("giaban")))
                    Catch ex As Exception
                        DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có giá cho thuốc này !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        Me.m_FlagValidate = True
                        Me.IGridViewChiTiet01.SetFocusedRowCellValue("dongiavnd", 0)
                    End Try
                End If
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có bảng giá nào áp dụng cho đối tượng :" & Me.TenkhachhangITextEdit.EditValue & " và thời gian '" & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "dd/MM/yyyy") & "'", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Me.m_FlagValidate = True
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Thuốc số lô hạn dùng này đã chọn, bạn phải chọn thuốc khác !!!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.m_FlagValidate = True
        End If
    End Sub
    Private Sub IGridViewChiTiet01_ShownEditor(sender As System.Object, e As System.EventArgs) Handles IGridViewChiTiet01.ShownEditor
        If Me.IGridViewChiTiet01.FocusedColumn.FieldName = "mafifo" Then
            'kiểm tra coi có tồn tại mặt hàng này chưa ?
            Me.GetTonKho_Lo_HanDung_KhaDungTableAdapter.Fill(Me.PCV_CLINICDataSetTongHop.getTonKho_Lo_HanDung_KhaDung, CDate(Me.TuNgayIDateEdit.EditValue), CDate(Me.DenNgayIDateEdit.EditValue))
        End If
        If Me.IGridViewChiTiet01.FocusedColumn.FieldName = "soluonggiao" Then
            Me.m_Flag = False
        End If
        Dim view As GridView = TryCast(sender, GridView)
    End Sub
    Private Sub IGridViewChiTiet01_CellValueChanging(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles IGridViewChiTiet01.CellValueChanging
        If Me.IGridViewChiTiet01.FocusedColumn.FieldName = "soluonggiao" Then
            Try
                Me.OldValue = CDec(Me.IGridViewChiTiet01.GetFocusedRowCellDisplayText("soluonggiao"))
            Catch ex As Exception
                Me.OldValue = 0
            End Try
        End If
    End Sub
    Private Sub BarButtonItemBaoCao_ItemClick_1(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemBaoCao.ItemClick
        Dim report As Report_PhieuXuatKho = New Report_PhieuXuatKho
        report.BhBanHangTableAdapter1.FillMa_BHBanHang(report.PcV_CLINICDataSet1.BHBanHang, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.BhBanHangChiTietTableAdapter1.FillMa_BHBanHangChiTiet(report.PcV_CLINICDataSet1.BHBanHangChiTiet, Me.IGridViewDs01.GetFocusedRowCellValue("ma"))
        report.NgayThangNam.Value = "Ngày " & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "dd") & " Tháng " & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "MM") & " Năm " & Format(CDate(Me.NgaychungtuIDateEdit.EditValue), "yyyy")
        report.ShowRibbonPreview()
    End Sub
    Private Sub IGridViewChiTiet01_ValidatingEditor(sender As Object, e As DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs) Handles IGridViewChiTiet01.ValidatingEditor
        If CInt(Me.GetGia_BangGiaApDungTableAdapter.getTonTaiBangGia(CDate(Me.NgaychungtuIDateEdit.EditValue), Me.MakhachhangISearchLookupEdit.EditValue).ToString) = 0 Then
            e.Valid = False
            e.ErrorText = "Chọn thuốc không hợp lệ !!!"
        End If
    End Sub

    Private Sub IGridViewChiTiet01_ValidateRow(sender As Object, e As Views.Base.ValidateRowEventArgs) Handles IGridViewChiTiet01.ValidateRow
        Dim view As GridView = TryCast(sender, GridView)
        Dim row As DataRowView = TryCast(e.Row, DataRowView)
        'kiểm tra coi có bảng giá tồn tại hay chưa 
        If CInt(Me.GetGia_BangGiaApDungTableAdapter.getTonTaiBangGia(CDate(Me.NgaychungtuIDateEdit.EditValue), Me.MakhachhangISearchLookupEdit.EditValue).ToString) = 0 Then
            e.Valid = False
            view.SetColumnError(view.Columns("mahang"), "Chưa có bảng giá áp dụng cho mặt hàng này !!!", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical)
            view.FocusedColumn = view.Columns("mahang")
        End If
    End Sub

    Private Sub IGridViewChiTiet01_FocusedColumnChanged(sender As Object, e As Views.Base.FocusedColumnChangedEventArgs) Handles IGridViewChiTiet01.FocusedColumnChanged
        Dim view As GridView = TryCast(sender, GridView)
        'kiểm tra coi có bảng giá tồn tại hay chưa 
        Try
            If CInt(Me.GetGia_BangGiaApDungTableAdapter.getTonTaiBangGia(CDate(Me.NgaychungtuIDateEdit.EditValue), Me.MakhachhangISearchLookupEdit.EditValue).ToString) = 0 Then
                view.SetColumnError(view.Columns("mahang"), "Chưa có bảng giá áp dụng cho mặt hàng này !!!", DevExpress.XtraEditors.DXErrorProvider.ErrorType.Critical)
                view.FocusedColumn = view.Columns("mahang")
            End If
        Catch ex As Exception

        End Try
    
    End Sub
End Class
